"""
Case Type   : HTAP行列融合特性
Case Name   : 指定分区转换后修改数据，验证查询数据正确
Create At   : 2025/1/8
Owner       : ningyali
Description :
    1.建表插入数据，并进行行列转换
    2.修改数据
    3.开启enable_imcsscan，explain和查询
    4.关闭enable_imcsscan，explain和查询
    5.清理数据环境
Expect      :
    1.执行成功
    2.执行成功
    3.执行成功, 查询计划走imcs scan
    4.执行成功，查询计划不走imcs scan,查询结果与第三步一致
    5.执行成功
History     :
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import macro


class HTAP(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_sh = CommonSH('SingleDbUser')
        self.Constant = Constant()
        self.common = Common()
        self.t_name = 't_htap_single_0142'

    def test_trans(self):
        text = '----step1:建表进行行列转换; expect:执行成功----'
        self.log.info(text)
        sql = f'drop table if exists {self.t_name};\n' \
              f'create table {self.t_name}(\n' \
              f'    c1 integer,\n' \
              f'    c2 varchar(30),\n' \
              f'    c3 char(30))\n' \
              f'partition by range (c1)(\n' \
              f'    partition {self.t_name}_p1 values less than (1000),\n' \
              f'    partition {self.t_name}_p2 values less than (2000),\n' \
              f'    partition {self.t_name}_p3 values less than (3000),\n' \
              f'    partition {self.t_name}_p4 values less than ' \
              f'(maxvalue));\n' \
              f'insert into {self.t_name} values (' \
              f'generate_series(1, 100000),' \
              f'generate_series(1, 100000)||\'aaa\',' \
              f'generate_series(1, 100000)||\'bbb\');' \
              f'alter table {self.t_name} modify partition ' \
              f'{self.t_name}_p4 imcstored;'
        sql_res = self.pri_sh.execut_db_sql(
            sql, env_path=macro.SINGLE_DB_ENV_PATH)
        expect_res = r'CREATE TABLE\s+INSERT 0 100000\s+ALTER TABLE'
        self.assertRegex(sql_res, expect_res, f'执行失败:{text}')

        text = '----step2:修改数据; expect:执行成功----'
        self.log.info(text)
        sql = f'update {self.t_name} set c3=c1||c3 where c1%3=0;' \
              f'\\d+'
        sql_res = self.pri_sh.execut_db_sql(
            sql, env_path=macro.SINGLE_DB_ENV_PATH)
        self.assertIn('UPDATE 33333', sql_res, f'执行失败:{text}')

        text = '----step3:开启enable_imcsscan，explain和查询; ' \
               'expect:执行成功, 查询计划走imcs scan----'
        self.log.info(text)
        sql = f'set enable_imcsscan to on;' \
              f'explain (costs false) select * from {self.t_name} ' \
              f'partition ({self.t_name}_p4) order by c1 limit 10;' \
              f'select * from {self.t_name} ' \
              f'partition ({self.t_name}_p4) order by c1 limit 10;'
        sql_res = self.pri_sh.execut_db_sql(
            sql, env_path=macro.SINGLE_DB_ENV_PATH)
        rows1, _ = self.common.parse_sql_query_res(sql_res)
        expect = [['3000', '3000aaa', '30003000bbb'],
                  ['3001', '3001aaa', '3001bbb'],
                  ['3002', '3002aaa', '3002bbb'],
                  ['3003', '3003aaa', '30033003bbb'],
                  ['3004', '3004aaa', '3004bbb'],
                  ['3005', '3005aaa', '3005bbb'],
                  ['3006', '3006aaa', '30063006bbb'],
                  ['3007', '3007aaa', '3007bbb'],
                  ['3008', '3008aaa', '3008bbb'],
                  ['3009', '3009aaa', '30093009bbb']]
        self.assertIn('IMCStore Scan', sql_res, f'执行失败:{text}')
        self.assertEqual(expect, rows1[-10:], f'执行失败:{text}')

        text = '----step4:关闭enable_imcsscan，explain和查询; ' \
               'expect:执行成功，查询计划不走imcs scan,查询结果与第三步一致----'
        self.log.info(text)
        sql = f'set enable_imcsscan to off;' \
              f'explain (costs false) select * from {self.t_name} ' \
              f'partition ({self.t_name}_p4) order by c1 limit 10;' \
              f'select * from {self.t_name} ' \
              f'partition ({self.t_name}_p4) order by c1 limit 10;'
        sql_res = self.pri_sh.execut_db_sql(
            sql, env_path=macro.SINGLE_DB_ENV_PATH)
        rows2, _ = self.common.parse_sql_query_res(sql_res)
        self.assertNotIn('IMCStore Scan', sql_res, f'执行失败:{text}')
        self.assertEqual(rows1[-10:], rows2[-10:], f'执行失败:{text}')

    def tearDown(self):
        text = '----step5:清理环境;expect:执行成功----'
        self.log.info(text)
        sql = f'drop table if exists {self.t_name};'
        sql_res = self.pri_sh.execut_db_sql(
            sql, env_path=macro.SINGLE_DB_ENV_PATH)
        self.assertIn('DROP TABLE', sql_res, f'修改表失败:{text}')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
